package com.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.pojo.FangAdmin;
import com.house.service.FangAdminService;
import com.house.mapper.FangAdminMapper;
import com.house.utils.R;
import com.house.vo.LoginVo;
import com.house.vo.RegisterVo;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author lenovo
 * @description 针对表【fang_admin(管理员表
 * )】的数据库操作Service实现
 * @createDate 2025-09-13 11:19:24
 */
@Service
public class FangAdminServiceImpl extends ServiceImpl<FangAdminMapper, FangAdmin>
        implements FangAdminService {

    @Autowired
    FangAdminMapper fangAdminMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public R register(RegisterVo registerVo) {
        // 检查用户名是否已存在
        FangAdmin fangAdmin = fangAdminMapper.selectOne(
                new LambdaQueryWrapper<FangAdmin>().eq(FangAdmin::getUsername, registerVo.getUsername())
        );

        if (fangAdmin != null) {
            return R.ERROR(500, "用户名已存在");
        }
        fangAdmin = new FangAdmin();
        fangAdmin.setUsername(registerVo.getUsername());
        // 加密密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(registerVo.getPassword());
        System.out.println("encodedPassword: " + encodedPassword);
        fangAdmin.setPassword(encodedPassword);

        // 设置其他属性
        fangAdmin.setRegTime(new Date());
        fangAdmin.setStatus(1);

        // 插入数据库
        fangAdminMapper.insert(fangAdmin);

        return R.OK(fangAdmin);
    }

    @Override
    public R login(LoginVo loginVo) {
        FangAdmin fangAdmin = fangAdminMapper.selectOne(
                new LambdaQueryWrapper<FangAdmin>().eq(FangAdmin::getUsername, loginVo.getUsername())
        );
        if (fangAdmin == null) {
            return R.ERROR(500, "用户名不存在");
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean password = encoder.matches(loginVo.getPassword(), fangAdmin.getPassword());
        if (!password) {
            return R.ERROR(500, "密码错误");
        }

        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", fangAdmin.getId())
                .claim("username", fangAdmin.getUsername()).compact();
        String redisToken = "login" + fangAdmin.getId();
        stringRedisTemplate.opsForValue().set(redisToken,token,60, TimeUnit.DAYS);
        fangAdmin.setToken(token);
        fangAdmin.setPassword(null);
        fangAdmin.setLastTime(new Date());
        fangAdmin.setLoginNum(fangAdmin.getLoginNum() + 1);

        fangAdminMapper.updateById(fangAdmin);
        // 登录成功
        return R.OK(fangAdmin);
    }
}